package robot.arm;
import com.sun.j3d.utils.geometry.*;

import com.sun.j3d.utils.universe.*;

import javax.media.j3d.*;

import javax.vecmath.*;
import java.applet.*;
import java.awt.GraphicsConfiguration;

public class Cylinders extends Applet {

	public Cylinders () {
		   // Create the universe

		   GraphicsConfiguration config =

			   SimpleUniverse.getPreferredConfiguration();

			   Canvas3D canvas = new Canvas3D(config);
		
		   //SimpleUniverse universe = new SimpleUniverse(canvas);
		   SimpleUniverse universe = new SimpleUniverse();

		   // Create a structure to contain objects

		   BranchGroup group = new BranchGroup();

		   // Create a ball and add it to the group of objects
		   
		   Appearance newAppearance = new Appearance();
		   
		   Color3f light1Color = new Color3f(1f, 1f, 1f);
		   
		   ColoringAttributes attr = new ColoringAttributes(light1Color, ColoringAttributes.SHADE_GOURAUD);
		   
		   newAppearance.setColoringAttributes(attr);

		   
		   
		      TransformGroup tg = new TransformGroup();

		      Transform3D transform = new Transform3D();

		      Vector3f vector = new Vector3f( 0f, -0.23f, 0f);

		      transform.setTranslation(vector);

		      tg.setTransform(transform);
		      
		      //newAppearance.setRenderingAttributes(null);
		      
		      ColoringAttributes prettyColors = new ColoringAttributes(255f, 255f, 0f, ColoringAttributes.NICEST);
		      
		      Material shiny = new Material();
		      
		      shiny.setAmbientColor(0.1f, 0.1f, 0f);
		      
		      shiny.setSpecularColor(0.1f, 0.1f, 0.1f);
		      
		      shiny.setDiffuseColor(1.0f, 1.0f, 0.0f);
		      
		      shiny.setShininess(0.05f);
		      
		      
		      newAppearance.setColoringAttributes(prettyColors);
		      newAppearance.setMaterial(shiny);
		      
		      // VERY IMPORTANT NOTE, YOU MUST HAVE THE GENERATE_NORMALS ENUM PASSED IN TO GET LIGHTING TO WORK!!!
		      Cylinder cylinder = new Cylinder(0.22f, 0.10f, Cylinder.GEOMETRY_NOT_SHARED | Cylinder.ENABLE_APPEARANCE_MODIFY | Cylinder.GENERATE_NORMALS, 25, 25, newAppearance);
		      //Cylinder cylinder = new Cylinder(0.33f, 0.19f);

		      Appearance otherAppearance = new Appearance();
		      
		      ColoringAttributes moreColors = new ColoringAttributes(255f, 0f, 255f, ColoringAttributes.NICEST);
		      
		      otherAppearance.setColoringAttributes(moreColors);
		      otherAppearance.setMaterial(shiny);
		      
		      Cylinder cylinder3 = new Cylinder(0.1f, 0.5f, Cylinder.GEOMETRY_NOT_SHARED | Cylinder.ENABLE_APPEARANCE_MODIFY | Cylinder.GENERATE_NORMALS, 25, 25, otherAppearance);
		      
		      
		      TransformGroup tg2 = new TransformGroup();
		      
		      tg2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      tg2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D transform2 = new Transform3D();

		      Vector3f vector2 = new Vector3f( 0f, 0.05f, 0f);

		      transform2.setTranslation(vector2);

		      tg2.setTransform(transform2);
		      
		      TransformGroup tg3 = new TransformGroup();
		      
		      tg3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      tg3.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D transform3 = new Transform3D();

		      Vector3f vector3 = new Vector3f( 0f, 0.65f, -0.5f);

		      transform3.setTranslation(vector3);

		      tg3.setTransform(transform3);

		   
		   Cylinder cylinder2 = new Cylinder(0.1f, .75f);
		   
		      ColoringAttributes moreColors2 = new ColoringAttributes(200f, 30f, 200f, ColoringAttributes.NICEST);
		      
		      otherAppearance.setColoringAttributes(moreColors2);
		      otherAppearance.setMaterial(shiny);
		      
		      Cylinder cylinder4 = new Cylinder(0.1f, 0.5f, Cylinder.GEOMETRY_NOT_SHARED | Cylinder.ENABLE_APPEARANCE_MODIFY | Cylinder.GENERATE_NORMALS, 25, 25, otherAppearance);
		   
		      TransformGroup tg4 = new TransformGroup();
		      
		      tg4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      tg4.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D transform4 = new Transform3D();

		      Vector3f vector4 = new Vector3f( 0f, 1.25f, -1.5f);

		      transform4.setTranslation(vector4);

		      tg4.setTransform(transform4);
		      
		      Cylinder cylinder5 = new Cylinder(0.1f, 0.25f, Cylinder.GEOMETRY_NOT_SHARED | Cylinder.ENABLE_APPEARANCE_MODIFY | Cylinder.GENERATE_NORMALS, 25, 25, otherAppearance);
			   
		      TransformGroup tg5 = new TransformGroup();
		      
		      tg5.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      tg5.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D transform5 = new Transform3D();

		      Vector3f vector5 = new Vector3f( 0f, 1.75f, -2.0f);

		      transform5.setTranslation(vector5);

		      tg5.setTransform(transform5);
		      
		      // now make the Spheres for joints
		      
		      Sphere joint1 = new Sphere(0.075f);
		      
		      Sphere joint2 = new Sphere(0.075f);
		      
		      Sphere joint3 = new Sphere(0.075f);
		      
		      Sphere joint4 = new Sphere(0.075f);

		      TransformGroup tg6 = new TransformGroup();
		      
		      tg6.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      tg6.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D transform6 = new Transform3D();

		      Vector3f vector6 = new Vector3f(0.0f, -0.25f, 0.0f);

		      transform6.setTranslation(vector6);

		      tg6.setTransform(transform6);
		      
		      TransformGroup tg7 = new TransformGroup();
		      
		      tg7.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      tg7.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D transform7 = new Transform3D();

		      Vector3f vector7 = new Vector3f(0.0f, 0.25f, -0.5f);

		      transform7.setTranslation(vector7);

		      tg7.setTransform(transform7);
		      
		      TransformGroup tg8 = new TransformGroup();
		      
		      tg8.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      tg8.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D transform8 = new Transform3D();

		      Vector3f vector8 = new Vector3f(0.0f, 0.5f, -1.0f);

		      transform5.setTranslation(vector8);

		      tg8.setTransform(transform8);
		      
		      TransformGroup tg9 = new TransformGroup();
		      
		      tg9.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      tg9.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D transform9 = new Transform3D();

		      Vector3f vector9 = new Vector3f(0.0f, 1.0f, -1.5f);

		      transform9.setTranslation(vector9);

		      tg9.setTransform(transform9);
		      
		   
		   tg.addChild(cylinder);
		   
		   group.addChild(tg);
		   
		   tg2.addChild(cylinder2);
		   
		   group.addChild(tg2);
		   
		   tg3.addChild(cylinder3);
		   
		   group.addChild(tg3);
		   
		   tg4.addChild(cylinder4);
		   
		   group.addChild(tg4);
		   
		   tg5.addChild(cylinder5);
		   
		   group.addChild(tg5);
		   
		   tg6.addChild(joint1);
		   
		   group.addChild(tg6);
		   
		   tg7.addChild(joint2);
		   
		   group.addChild(tg7);
		   
		   tg8.addChild(joint3);
		   
		   group.addChild(tg8);
		   
		   tg9.addChild(joint4);
		   
		   group.addChild(tg9);
		   
		   // now make end effector:
		   
		   
		   Box clawBase = new Box(1.5f, 0.25f, 0.1f, newAppearance);
		   
		   Box clawLeft = new Box(0.25f, 0.25f, 0.1f, newAppearance);
		   Box clawRight = new Box(0.25f, .25f, 0.1f, newAppearance);
		   
		      TransformGroup cBase = new TransformGroup();
		      
		      cBase.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      cBase.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D cbTrans = new Transform3D();

		      Vector3f cbVector = new Vector3f( 0f, 1.9f, -2.0f);

		      cbTrans.setTranslation(cbVector);

		      cBase.setTransform(cbTrans);
		      
		      cBase.addChild(clawBase);
		      
		      group.addChild(cBase);

		      TransformGroup cLeft = new TransformGroup();
		      
		      cLeft.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      cLeft.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D clTrans = new Transform3D();

		      Vector3f clVector = new Vector3f( -0.5f, 1.9f, -2.0f);

		      clTrans.setTranslation(clVector);

		      cLeft.setTransform(clTrans);
		      
		      cLeft.addChild(clawLeft);
		      
		      group.addChild(cLeft);
		      
		      TransformGroup cRight = new TransformGroup();
		      
		      cRight.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      cRight.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D crTrans = new Transform3D();

		      Vector3f crVector = new Vector3f( -0.5f, 1.9f, -2.0f);

		      crTrans.setTranslation(crVector);

		      cRight.setTransform(crTrans);
		      
		      cRight.addChild(clawRight);
		      
		      group.addChild(cRight);
		      
		   

		   // Create a red light that shines for 100m from the origin


		   
		   BoundingSphere bounds =

			   new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

			   Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);

			   DirectionalLight light1

			      = new DirectionalLight(light1Color, light1Direction);

			   light1.setInfluencingBounds(bounds);

			   group.addChild(light1);

		   
		   universe.getViewingPlatform().setNominalViewingTransform();

		   universe.addBranchGraph(group);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Cylinders cylinders = new Cylinders();
	}

}
